<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/header.jsp" %>
<script type="text/javascript">
var treeNode = zTree.getSelectedNodes()[0];
	var data = $("#OrgOpenFlag").val();
	//alert(data);
	initButton(data);
	//时间插件切换目录后失效，加载前清下$("#ui-datepicker-div")
	$("#ui-datepicker-div").remove();
	 $("#foundDate").datepicker({
	maxDate:new Date(),
	changeMonth: true,
    changeYear: true,
    showOtherMonths: true,
    selectOtherMonths: true,
    dateFormat:'yy-mm-dd',
    showButtonPanel:false,//是否显示按钮面板  
    showMonthAfterYear:true,//是否把月放在年的后面 
    onSelect: function() {//选择日期后执行的操作
       	//什么也不做，可以扩展
    },
    clearText:"清除",//清除日期的按钮名称  
    closeText:"关闭",//关闭选择框的按钮名称  
    yearSuffix: '年', //年的后缀 
    monthNames: ['一月','二月','三月','四月','五月','六月',
         		'七月','八月','九月','十月','十一月','十二月'],
    monthNamesShort: ['一月','二月','三月','四月','五月','六月',
               		'七月','八月','九月','十月','十一月','十二月'],
    dayNames: ['周日','周一','周二','周三','周四','周五','周六'],
      
}); 
	$("#orgDetailForm").validate({
	    errorElement: 'div',
	    errorClass: 'help-block',
	    focusInvalid: false,
	    rules: {
	    	"orgInfo.orgId": {
	          required: true,
	          minlength:'8'
	       },
	       "orgInfo.orgName": {
		          required: true
		       }
	       ,
	       "orgInfo.orgLevelName":{
	    	   required: true
	       },
	       "orgInfo.status": {
		          required: true
		       }
	    },
	    messages: {
	    	"orgInfo.orgId": {
	           required: "机构编号必须为8位且不含中文！"
	        },
	        "orgInfo.orgName": {
		           required: "机构名称不能为空！"
		        } ,
		        "orgInfo.orgLevelName":{
			    	   required: "机构级别不能为空！"
			       },
		        "orgInfo.status": {
			           required: "机构状态不能为空！"
			        }
	    },
	    highlight: function (e) {
	        $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
	    },
	    success: function (e) {
	        $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
	        $(e).remove();
	    },
	    errorPlacement: function (error, element) {
	        error.insertAfter(element.parent());
	    }
	});	
	var orgProductStr=$("#orgProductStr").val();
	//~~~~~~~~~~~~~~~~~ 勾选默认产品 START~~~~~~~~~~~~~
	/* var orgProductStrArray = orgProductStr.split(',');
 	var productArray=new Array();
	$("#productModifyInfo :input").each(function(){
		var cur=$(this).val();
		productArray.push(cur);
	});
	$.each(orgProductStrArray,function(i,value){
		if($.inArray(value,productArray)!=-1){
			$("#productModifyInfo input[id=productId"+value+"]").prop('checked', true);
		}
 	}); */
	//~~~~~~~~~~~~~~~~~ 勾选默认产品END ~~~~~~~~~~~~~
	
//初始化按钮显示
function initButton(data){
	if(data == "1"){//新增
		$("#btnSave").show();
		$("#btnCancle").show();
		$("#btnModify").hide();
		$("#btnDelete").hide();
		$("#org_creater").hide();
		$("#org_createTime").hide();
		$("#org_updater").hide();
		$("#org_updateTime").hide();
		enabledAll();
	}else if(data == "3"){//查询
		$("#btnSave").hide();
		$("#btnCancle").hide();
		$("#btnModify").show();
		$("#btnDelete").show();
		disabledAll();
	}else{//修改
		$("#btnSave").show();
		$("#btnCancle").show();
		$("#btnModify").hide();
		$("#btnDelete").hide();
		enabledAll();
		
	}
}
//设置文本框不可改动
function disabledAll(){
	$("#orgDetailForm input[type='text']").attr("disabled",true);
	$("#orgDetailForm input[type='tel']").attr("disabled",true);
	$("#orgDetailForm input[type='checkbox']").attr("disabled",true);
	$("#orgDetailForm select").attr("disabled",true);
}
//
function enabledAll(){
	$("#orgDetailForm input[type='text']").attr("disabled",false);
	$("#orgDetailForm input[type='tel']").attr("disabled",false);
	$("#orgDetailForm input[type='checkbox']").attr("disabled",false);
	$("#orgDetailForm select").attr("disabled",false);
}
//点击修改按钮后显示的设置
$("#btnModify").click(function(){
	$("#btnSave").show();
	$("#btnCancle").show();
	$("#btnModify").hide();
	$("#btnDelete").hide();
	$("#openFlag").val("2");
	$("#orgId").attr("readonly",true);
	$("#orgName").attr("readonly",true);
	enabledAll();
});
//取消修改
$("#btnCancle").click(function(){
	var data = $("#OrgOpenFlag").val();
	if(data =="1"){
		$("#orgDiv").hide();
		return;
	}
	var iscancle=window.confirm("确定取消修改？");
	if(iscancle==true){
		document.orgDetailForm.reset();
		$("#btnSave").hide();
		$("#btnCancle").hide();
		$("#btnModify").show();
		$("#btnDelete").show()
		disabledAll();
	}
	else{
		return;
	}
});
//点击保存按钮后进行的操作
$("#btnSave").click(function(){
	if ($("#orgDetailForm").valid() == false) {
		return;
    }
	var openFlag = $("#OrgOpenFlag").val();
	var urlVal ="${ctx}/org/saveOrgInfo";
	saveOrgInfo(urlVal);
	return;
});
//异步保存信息
function saveOrgInfo(urlVal){
	var products = $("input[name^=products_]:checked");
	var product_str = "";
	var custManagerMobile_str = "";
	$.each(products, function(i, v){
		product_str = product_str + "," + $(v).val();
		custManagerMobile_str = custManagerMobile_str + ", " + $("#custManagerMobile"+$(v).val()).val();
	});
	var message;
	var params ={
			"orgId":$("#orgId").val(),
			"orgName":$("#orgName").val(),
			"orgCode":$("#orgCode").val(),
			"treeLevel":$("#treeLevel").val(),
			"hasChild":$("#hasChild").val(),
			"orgFullName":$("#orgFullName").val(),
			"orgLevel":$("#orgLevelName").val(),
			"status":$("#status").val(),
			//"orgType":$("#orgType").val(),
			"parentId":$("#parentId").val(),
			"linkTel":$("#linkTel").val(),
			"linkMan":$("#linkMan").val(),
			"linkAddress":$("#linkAddress").val(),
			"foundDate":$("#foundDate").val(),
			"createUser":$("#createUser").val(),
			"createTime":$("#createTime").val(),
			"updateUser":$("#updateUser").val(),
			"updateTime":$("#updateTime").val(),
			//"orgAttr":$("#orgAttr").val(),
			"toOrgId":$("#toOrgId").val(),
			"openFlag":$("#OrgOpenFlag").val(),
			"addressLandmark":$("#addressLandmark").val(),
			"custManagerMobile":custManagerMobile_str,
			"productId":product_str
	};
	enabledAll();
	$.ajax({
		   async : false,
			url :urlVal ,
			type : "post",
			data : $.param(params),
			dataType : "json",
			success : function(data){
		      alert(data.success);
		      initButton("3");
		      freshTreeNode(treeNode);
		      freshOrgPage();
		      $("#deptDetailForm").load("${ctx}/org/orgDetail",param);
		  }
	 });
}

//点击删除按钮进行删除机构操作（未启用的机构并且机构下面没有子节点的机构才能进行删除操作）
$("#btnDelete").click(function(){
	if($("#status").val()!='2'){
		alert("只能删除停用的机构！");
		return;
	}
	if(!window.confirm("您确定要删除该机构吗？")){
          return ;//点击“取消”时，关闭弹出框，不再进行其他操作
    }
	var params ={
			"orgId":$("#orgId").val(),
			"orgName":$("#orgName").val(),
			"orgFullName":$("#orgFullName").val(),
			"orgLevel":$("#orgLevelName").val(),
			"status":$("#status").val(),
			//"orgType":$("#orgType").val(),
			"parentId":$("#parentId").val(),
			"linkTel":$("#linkTel").val(),
			"linkMan":$("#linkMan").val(),
			"linkAddress":$("#linkAddress").val(),
			"foundDate":$("#foundDate").val(),
			"createUser":$("#orgCreateUser").val(),
			"createTime":$("#orgCreateTime").val(),
			"updateUser":$("#orgUpdateUser").val(),
			"updateTime":$("#orgUpdateTime").val(),
			//"orgAttr":$("#orgAttr").val(),
			"toOrgId":$("#toOrgId").val()
	};
	 $.ajax({
		   async : false,
			url : "${ctx}/org/deleteOrg",
			type : "post",
			data : $.param(params),
			dataType : "json",
			success : function(data){
				alert(data.success);
			     $("#orgDiv").hide();
			     var param = {"checkType":"dept","isCheckOrg":"1"};
			 	$("#myTreeDiv").load("${ctx}/org/haveRootNodes",param);	
		  },
		  error: function(ret){
	            alert(ret.responseText);
	            disabledAll();
	      }
	 });
});

function freshTreeNode(treeNode){
	//debugger;
	if(treeNode!=null&& treeNode.parentTId!=null){
		var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
		if(treeNode.children != null ){
			zTree.reAsyncChildNodes(treeNode, "refresh");
			//alert("刷新treeNode");
		}else{
			zTree.reAsyncChildNodes(treeNodeP, "refresh");
			//alert("刷新treeNodeP");
		}
	}else{
		var param = {"checkType":"dept","isCheckOrg":"1"};
		$("#myTreeDiv").load("${ctx}/org/haveRootNodes?"+new Date().getTime(),param);
	}
}

function freshOrgPage(){
	var param ={"orgId": $("#orgId").val()};
	$("#deptDetailForm").show();
	$("#deptDetailForm").load("${ctx}/org/orgDetail",param);
}
</script>

<div class="row" id="orgDiv">
	<div class="col-xs-12">	
		<div class="widget-box widget-color-blue2">			
			<div class="widget-header">
				<h5 class="widget-title">机构信息</h5>
				<!-- #section:custom/widget-box.toolbar -->
				<div class="widget-toolbar">
					<a href="#" data-action="fullscreen" class="orange2"><i class="ace-icon fa fa-expand"></i></a>
					<a href="#" data-action="collapse"><i class="ace-icon fa fa-chevron-up"></i></a>
					<a href="#" data-action="close"><i class="ace-icon fa fa-times"></i></a>
				</div>
				<!-- /section:custom/widget-box.toolbar -->
			</div>
			<div class="widget-body">
				<div class="widget-main no-padding">				
					<form class="form-horizontal" id="orgDetailForm" name="orgDetailForm">
					<input type="hidden" id="OrgOpenFlag" name="openFlag" value="${openFlag}"/>
					 <input type="hidden" name="orgInfo.treeLevel" id="treeLevel" value="${orgInfo.treeLevel}" />
					 <input type="hidden" name="parentId" id="parentId" value="${orgInfo.parentId}" />
					  <input type="hidden" name="orgInfo.hasChild" id="hasChild" value="${orgInfo.hasChild}" />
					   <input type="hidden" name="toOrgId" id="toOrgId" value="${orgInfo.toOrgId}" />
					   <input type="hidden" name="orgCode" id="orgCode" value="${orgInfo.orgCode}" />
						<fieldset>
						    <div class="row">
						    	<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="userId">机构代码<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">	
										<input type="text" id="orgId" name="orgInfo.orgId" 
					                        value="${orgInfo.orgId}" maxlength="8" title='机构编号必须为8位且不含中文' class="col-xs-12" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')"/>
									</div>
									</div> 
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="orgLevelName">机构级别<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">
										<select  class="chosen-select col-xs-12"  id="orgLevelName" name="orgInfo.orgLevelName" disabled>
										<hw:dictOption dictType="D00028" value="${orgInfo.orgLevel}"/>
										</select>
									</div>
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="orgName">机构名称<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">
										<input type="text" id="orgName" name="orgInfo.orgName" value="${orgInfo.orgName}" class="col-xs-12" title="${orgInfo.orgName}"/>
									</div>
									</div>
								</div>
						    	<%-- <div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="orgFullName">机构地址</label>
									<div class="col-sm-8">
										<input type="text" id="orgFullName" name="orgInfo.orgFullName" value="${orgInfo.orgFullName}" class="col-xs-12"  />
									</div>
								</div> --%>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="parentIdName">上级机构：</label>
									<div class="col-sm-8">
										<input  type="text" class="col-xs-12" id="parentIdName" name="orgInfo.parentId" value="<hw:orgName orgId='${orgInfo.parentId}'/>" readonly />
									</div>
								</div>							
								<%-- <div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="toOrgName">统计归属机构</label>
									<div class="col-sm-8">
										<input type="text" id="toOrgName" name="toOrgName" value="<hw:orgName orgId='${orgInfo.toOrgId}'/>" class="col-xs-12" readonly />
									</div>
								</div> --%>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="status">机构状态<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">
										<select class="chosen-select col-xs-12" id="status" name="orgInfo.status" >
										<hw:dictOption dictType="D00027" value="${orgInfo.status}" />
										</select>
										</div>
									</div>
								</div>
								<%-- <div class="form-group col-xs-6" id="org_creater">
									<label class="col-sm-4 control-label no-padding-left" for="orgCreateUser">创建人</label>
									<div class="col-sm-8">
										<input type="text" id="orgCreateUser" name="orgInfo.createUser" value='<hw:userName userId='${orgInfo.createUser}'/>' class="col-xs-12" readonly/>
									</div>
								</div> --%>
								<div class="form-group col-xs-6" id="org_updater">
									<label class="col-sm-4 control-label no-padding-left" for="orgUpdateUser">修改人：</label>
									<div class="col-sm-8">
										<input type="text" id="orgUpdateUser" name="orgInfo.updateUser" value='<hw:userName userId='${orgInfo.updateUser}'/>' class="col-xs-12" readonly/>
									</div>
								</div>		
								<%-- <div class="form-group col-xs-6" id="org_createTime">
									<label class="col-sm-4 control-label no-padding-left" for="orgCreateTime">创建时间</label>
									<div class="col-sm-8">
										<input type="text" id="orgCreateTime" name="orgInfo.createTime" value="${orgInfo.createTime}" class="col-xs-12" readonly/>
									</div>
								</div> --%>	
								<div class="form-group col-xs-6" id="org_updateTime">
									<label class="col-sm-4 control-label no-padding-left" for="orgUpdateTime">修改时间：</label>
									<div class="col-sm-8">
										<input type="text" id="orgUpdateTime" name="orgInfo.updateTime" value="${orgInfo.updateTime}" class="col-xs-12" readonly/>
									</div>
								</div>
								<div class="form-group col-xs-6" id="address_Landmark">
									<label class="col-sm-4 control-label no-padding-left" for="addressLandmark">地标：</label>
									<div class="col-sm-8">
										<input type="text" id="addressLandmark" name="orgInfo.addressLandmark" value="${orgInfo.addressLandmark}" class="col-xs-12" />
									</div>
								</div>
								
								
								<c:forEach items="${products}" var="product" varStatus="i">


								<div class="form-group col-xs-12" id="address_Landmark">
									<label class="col-sm-2 control-label no-padding-left" for="addressLandmark">产品<font color="red">*</font></label>
									<div class="col-sm-8">
										<label style="min-width:90px">
											<input type="checkbox" style="margin-left:0px;" class="ace ace-checkbox-2 no-margin-left products " data-val="true" name="products_${product.productId}" 
												value="${product.productId}"  id="productId${product.productId}"
												<c:choose>
													<c:when test="${product.checked == '1'}"> 
														checked="checked"
													</c:when>
												</c:choose> 
											/> 
											<span class="lbl padding-2">${product.productName}</span>
										</label>
										&nbsp;&nbsp;&nbsp;&nbsp;
										<input type="tel" id="custManagerMobile${product.productId}" value="${product.tele}" placeholder="${product.productName}主管电话" maxlength="15"/>
									</div>

									 <div class="form-group  col-sm-2">
									</div> 
									
									<!-- <div class="form-group col-xs-6" id="address_Landmark">
										<div class="col-sm-8">
											<input type="text" id="addressLandmark" name="orgInfo.addressLandmark" value="${orgInfo.addressLandmark}" class="col-xs-12" />
										</div>
									</div> -->

									<!-- <label class="col-sm-2 control-label no-padding-left" for="addressLandmark">产品<font color="red">*</font></label>
									<div class="col-sm-4">
										<label style="width: 100px">
											<input type="checkbox" style="margin-left:0px;" class="ace ace-checkbox-2 no-margin-left products " data-val="true" name="products_${product.productId}" 
												value="${product.productId}"  id="productId${product.productId}"
												<c:choose>
													<c:when test="${product.checked == '1'}"> 
														checked="checked"
													</c:when>
												</c:choose> 
											/> 
											<span class="lbl padding-2">${product.productName}</span>
										</label>
										&nbsp;&nbsp;&nbsp;&nbsp;
										<input type="tel" id="custManagerMobile${product.productId}" value="${product.tele}" placeholder="${product.productName}主管电话" maxlength="15"/>
									</div>
									<div class="col-sm-6">
									</div> -->
								</div>
								</c:forEach>
								
							</div>
						</fieldset>
						<div class="form-actions center">
						<div class="btn-group">
							<button class="btn btn-success btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"  type="button" id="btnSave">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-floppy-o bigger-110  "></i>
								保存
								</span>
							</button>
							</div>
							&nbsp; &nbsp; &nbsp;
							<div class="btn-group">
							<button class="btn btn-primary btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" id="btnModify">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>
								修改
								</span>
							</button>
							</div>
							&nbsp; &nbsp; &nbsp;
							<div class="btn-group">
							<button class="btn btn-danger btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" id="btnDelete">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-trash-o bigger-110"></i>
								删除
								</span>
							</button>
							</div>
							&nbsp; &nbsp; &nbsp;
							<div class="btn-group">
							<button class="btn btn-warning btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" id="btnCancle">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-undo bigger-110"></i>
								取消
								</span>
							</button>
							</div>
						</div>	
						<input type="hidden" id=orgProductStr value="${orgProductStr}">  	
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
